using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/*
* @Author: zhaocg
* @Description:
* @Date: 2025年03月17日 星期一 13:03:47
* @Modify:
*/
public class CameraController : MonoBehaviour
{
    public float moveSpeed = 5.0f; // 相机移动速度
    public float mouseSensitivity = 300.0f; // 鼠标灵敏度

    public bool isKeyboardLocked = false;
    public bool isMouseLocked = false;
    
    private float xRotation = 0f;   //存储相机的euler角
    private float yRotation = 0f;   //存储相机的euler角
    
    private bool isDragging = false;
    private Vector3 dragStartPosition;
    private Vector3 initialCameraPosition;
    private Vector3 previousMousePosition;
    public float dragSensitivity = 1f;
    
    private bool isRotating = false; // 是否处于旋转状态
    public float rotateSensitivity = 2; 
    
    public float zoomSpeed = 100.0f;
    public float minZoomDistance = 10f;
    public float maxZoomDistance = 1000f;
    
    private Transform _positionRoot;

    public void Init()
    {
        isKeyboardLocked = false;
        isMouseLocked = false;
        xRotation = transform.eulerAngles.x;
        yRotation = transform.eulerAngles.y;
        
    }
    
    void Update()
    {
        if (!isKeyboardLocked)
        {
            // 获取键盘输入
            float moveX = Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime;
            float moveZ = Input.GetAxis("Vertical") * moveSpeed * Time.deltaTime;

            // 移动相机
            transform.Translate(moveX, 0, moveZ);
        }

        if (!isMouseLocked)
        {
            if (Input.GetMouseButtonDown(2))
            {
                isDragging = true;
                dragStartPosition = Input.mousePosition;
                initialCameraPosition = transform.position;
                previousMousePosition = Input.mousePosition;
            }
            else if (Input.GetMouseButtonUp(2))
            {
                isDragging = false;
            }
            
            // 右键旋转逻辑
            if (Input.GetMouseButtonDown(1)) // 右键按下开始旋转
            {
                isRotating = true;
                previousMousePosition = Input.mousePosition;
            }
            else if (Input.GetMouseButtonUp(1))
            {
                isRotating = false;
            }
            
            float scrollDelta = Input.GetAxis("Mouse ScrollWheel");
            if (scrollDelta != 0)
            {
                Vector3 moveDirection = transform.forward * scrollDelta * zoomSpeed * Time.deltaTime;
                transform.position += moveDirection;
                // if (Vector3.Distance(transform.position, _positionRoot.position) < minZoomDistance)
                // {
                //     transform.position = _positionRoot.position + transform.forward * minZoomDistance;
                // }
                //
                // if (Vector3.Distance(transform.position, _positionRoot.position) > maxZoomDistance)
                // {
                //     transform.position = _positionRoot.position + transform.forward * maxZoomDistance;
                // }
            }

            if (isDragging)
            {
                Vector3 currentMousePos = Input.mousePosition;
                Vector3 delta = currentMousePos - previousMousePosition;

                float sensitivity = dragSensitivity;
                // Vector3 moveDirection = delta.x * transform.right * sensitivity * Time.deltaTime + delta.y * -transform.up * sensitivity * Time.deltaTime;
                Vector3 moveDirection = delta.x * transform.right * sensitivity * Time.deltaTime;

                transform.position += moveDirection;
                previousMousePosition = currentMousePos;
            }
            
            if (isRotating)
            {
                Vector3 currentMousePos = Input.mousePosition;
                Vector3 delta = currentMousePos - previousMousePosition;
                
                float mouseXDelta = delta.x * rotateSensitivity * Time.deltaTime;
                float mouseYDelta = delta.y * rotateSensitivity * Time.deltaTime;
                yRotation += mouseXDelta; // 绕Y轴左右旋转
                xRotation -= mouseYDelta; // 绕X轴上下旋转
                // xRotation = ClampAngle(xRotation, -80, 80);
                // yRotation = ClampAngle(yRotation, -360, 360);
                transform.eulerAngles = new Vector3(xRotation, yRotation, transform.eulerAngles.z);

                previousMousePosition = currentMousePos;
            }
        }
    }
    
    //将angle限制在min~max之间
    private float ClampAngle(float angle, float min, float max)
    {
        return Mathf.Clamp(angle, min, max);
    }
}
